package com.neusoft.news.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.news.bean.User;
import com.neusoft.news.service.UserService;

/**
 * Servlet implementation class UserUpdateServlet
 */
@WebServlet("/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		boolean result = false;
		request.setCharacterEncoding("UTF-8");
		String userid = request.getParameter("userid");
		String username = request.getParameter("username");
		String usersex = request.getParameter("sex");
		String brithday = request.getParameter("brithday");
		String tel = request.getParameter("tel");
		String address = request.getParameter("address");
		
		User user = new User();
		
		user.setUserId(Integer.parseInt(userid));
		user.setUserName(username);
		user.setUserSex(usersex);
		user.setUserBrithday(brithday);
		user.setUserTel(tel);
		user.setUserAddr(address);
		
		UserService service = new UserService();
		try {
			result = service.update(user);
			if(result == true) {
				request.setAttribute("user",user);
				request.getRequestDispatcher("user.jsp").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.getRequestDispatcher("error2.jsp").forward(request, response);
		}
		
	}

}
